package com.markus.code.图.图1;

import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;

/**
 * Author:markusZhang
 * Date:Create in 2020/8/19 11:37
 * todo: 广度优先遍历
 */
public class GraphBfs {
    public void bfs(Node node){
        if (node == null){
            return ;
        }
        Deque<Node> deque = new LinkedList<>();
        HashSet<Node> set = new HashSet<>();
        deque.addLast(node);
        set.add(node);
        while(!deque.isEmpty()){
            Node cur = deque.pollFirst();
            System.out.print(cur.value+" ");
            for (Node next : cur.nexts) {
                if (!set.contains(next)){
                    deque.addLast(next);
                    set.add(next);
                }
            }
        }
    }
}
